package model

import (
	"gowork/configurationLog/utils"
	"time"
)

type Rule struct {
	Id int64 `xorm:"pk autoincr"`
	OrderType int
	Department string
	OCOrderInterval int
	OCSingleOrderMaxAmount int64
	OCSOMAExclusivePackage string
	OCPerMonthOrderMaxAmount int64
	OCPMOMAExclusivePackage string
	OCCumulativeOrderMaxAmount int64
	OCCOMAExclusivePackage string
	INCOrderInterval int
	INCSingleOrderMaxAmount int64
	INCSOMAExclusivePackage string
	INCPerMonthOrderMaxAmount int64
	INCPMOMAExclusivePackage string
	INCCumulativeOrderMaxAmount int64
	INCCOMAExclusivePackage string
	State int  //  1启用  2 停用
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ProductPurchaseRestrictions struct {
	Id int64 `xorm:"pk autoincr"`
	ProductId int64
	ProductName string
	IntervalRestriction int // 1 勾选   2  未勾选
	Interval int
	RestrictionType int
	IntervalLimit int
	SumRestriction int // 1 勾选   2  未勾选
	SumLimit int
	State int  //  1启用  2 停用
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ConfigurationLog struct {
	Id int64 `xorm:"pk autoincr"`
	ProductOrderRestrictionId int64
	Account string
	Department string
	OrderType int
	OperationType int
	ProductId int64
	ProductName string
	LogType int
	State int
	OrderEveryMonth int64
	ExcludeProducts string
	CumulativeOrders int64
	SingleOrder int64
	SingleInterval int
	IntervalLimitStatus int
	IntervalLimit int
	TotalLimitStatus int
	TotalLimit int
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (configurationLog *ConfigurationLog)AddModifyRules(account string, rule *Rule, restrictions *ProductPurchaseRestrictions, rt int, cu int)(map[string]interface{}, error){

	w := new(utils.Worker)

	var cl = ConfigurationLog{
		Id:                        w.GetId(),
		Account:                   account,
		CreatedAt:                 time.Now(),
		UpdatedAt:                 time.Now(),
	}

	cl.OperationType = cu

	if rt == 1{

		if cu == 1 {
			rule.Id = w.GetId()
			rule.CreatedAt = time.Now()
			rule.UpdatedAt = time.Now()
			x.Insert(rule)
		}else {
			rule.UpdatedAt = time.Now()
			x.Id(rule.Id).Update(rule)
		}

		cl.ProductOrderRestrictionId = rule.Id
		cl.LogType = 1
		cl.Department = rule.Department
		cl.OrderType = rule.OrderType
		cl.State = rule.State
		cl.OrderEveryMonth = rule.OCPerMonthOrderMaxAmount
		cl.ExcludeProducts = rule.OCCOMAExclusivePackage + rule.OCPMOMAExclusivePackage + rule.OCSOMAExclusivePackage
		cl.CumulativeOrders = rule.OCCumulativeOrderMaxAmount
		cl.SingleOrder = rule.OCSingleOrderMaxAmount
		cl.SingleInterval = rule.OCOrderInterval
	}else {

		if cu == 1 {
			restrictions.Id = w.GetId()
			restrictions.CreatedAt = time.Now()
			restrictions.UpdatedAt = time.Now()
			x.Insert(restrictions)
		}else {
			restrictions.UpdatedAt = time.Now()
			x.Id(restrictions.Id).Update(restrictions)
		}

		cl.ProductOrderRestrictionId = restrictions.Id
		cl.LogType = 2
		cl.ProductId = restrictions.ProductId
		cl.ProductName = restrictions.ProductName
		cl.IntervalLimitStatus = restrictions.IntervalRestriction
		cl.IntervalLimit = restrictions.IntervalLimit
		cl.TotalLimitStatus = restrictions.SumRestriction
		cl.TotalLimit = restrictions.SumLimit
	}

	x.Insert(&cl)

	return map[string]interface{}{"message":"成功","code":200}, nil
}

func (configurationLog *ConfigurationLog)ViewLog()([]*ConfigurationLog, error){

	var clList []*ConfigurationLog
	x.Desc("created_at").Find(&clList)

	return clList, nil
}

func ErrorHandler(clList *[]*ConfigurationLog, nclList *[]*ConfigurationLog, index int){
	defer func() {
		if err := recover(); err != nil {
			return
		}
	}()

	*nclList = append(*nclList, (*clList)[index])
	*nclList = append(*nclList, (*clList)[index+1])
}

type LogDetails struct {
	Name string
	NewValue interface{}
	OldValue interface{}
	Modify bool
}

func (configurationLog *ConfigurationLog)ViewLogDetails(clId int64, porId int64)([]*LogDetails, error){

	var cl ConfigurationLog
	x.Id(clId).Get(&cl)

	var clList []*ConfigurationLog
	x.Where("product_order_restriction_id = ?", porId).Desc("created_at").Find(&clList)

	var index int = 0
	for _, cl := range clList{
		if cl.Id == clId{
			break
		}
		index += 1
	}

	var nclList []*ConfigurationLog

	ErrorHandler(&clList, &nclList, index)

	var ldList []*LogDetails

	if cl.LogType == 1{

		if len(nclList) > 1 {

			var ldState = LogDetails{
				Name:     "状态",
				NewValue: nclList[0].State,
				OldValue: nclList[1].State,
			}
			if nclList[0].State != nclList[1].State {
				ldState.Modify = true
			}
			ldList = append(ldList, &ldState)

			var ldOrderEveryMonth = LogDetails{
				Name:     "每月下单",
				NewValue: nclList[0].OrderEveryMonth,
				OldValue: nclList[1].OrderEveryMonth,
			}
			if nclList[0].OrderEveryMonth != nclList[1].OrderEveryMonth {
				ldOrderEveryMonth.Modify = true
			}
			ldList = append(ldList, &ldOrderEveryMonth)

			var ldExcludeProducts = LogDetails{
				Name:     "排除产品",
				NewValue: nclList[0].ExcludeProducts,
				OldValue: nclList[1].ExcludeProducts,
			}
			if nclList[0].ExcludeProducts != nclList[1].ExcludeProducts {
				ldExcludeProducts.Modify = true
			}
			ldList = append(ldList, &ldExcludeProducts)

			var ldCumulativeOrders = LogDetails{
				Name:     "累计下单",
				NewValue: nclList[0].CumulativeOrders,
				OldValue: nclList[1].CumulativeOrders,
			}
			if nclList[0].CumulativeOrders != nclList[1].CumulativeOrders {
				ldCumulativeOrders.Modify = true
			}
			ldList = append(ldList, &ldCumulativeOrders)

			var ldSingleOrder = LogDetails{
				Name:     "单笔订单",
				NewValue: nclList[0].SingleOrder,
				OldValue: nclList[1].SingleOrder,
			}
			if nclList[0].SingleOrder != nclList[1].SingleOrder {
				ldSingleOrder.Modify = true
			}
			ldList = append(ldList, &ldSingleOrder)

			var ldSingleInterval = LogDetails{
				Name:     "下单间隔",
				NewValue: nclList[0].SingleInterval,
				OldValue: nclList[1].SingleInterval,
			}
			if nclList[0].SingleInterval != nclList[1].SingleInterval {
				ldSingleInterval.Modify = true
			}
			ldList = append(ldList, &ldSingleInterval)
		}else {
			var ldState = LogDetails{
				Name:     "状态",
				NewValue: nclList[0].State,
			}
			ldList = append(ldList, &ldState)

			var ldOrderEveryMonth = LogDetails{
				Name:     "每月下单",
				NewValue: nclList[0].OrderEveryMonth,
			}
			ldList = append(ldList, &ldOrderEveryMonth)

			var ldExcludeProducts = LogDetails{
				Name:     "排除产品",
				NewValue: nclList[0].ExcludeProducts,
			}
			ldList = append(ldList, &ldExcludeProducts)

			var ldCumulativeOrders = LogDetails{
				Name:     "累计下单",
				NewValue: nclList[0].CumulativeOrders,
			}
			ldList = append(ldList, &ldCumulativeOrders)

			var ldSingleOrder = LogDetails{
				Name:     "单笔订单",
				NewValue: nclList[0].SingleOrder,
			}
			ldList = append(ldList, &ldSingleOrder)

			var ldSingleInterval = LogDetails{
				Name:     "下单间隔",
				NewValue: nclList[0].SingleInterval,
			}
			ldList = append(ldList, &ldSingleInterval)
		}

	}else {

		if len(nclList) > 1 {
			var ldIntervalLimitStatus = LogDetails{
				Name:     "间隔限制状态",
				NewValue: nclList[0].IntervalLimitStatus,
				OldValue: nclList[1].IntervalLimitStatus,
			}
			if nclList[0].IntervalLimitStatus != nclList[1].IntervalLimitStatus {
				ldIntervalLimitStatus.Modify = true
			}
			ldList = append(ldList, &ldIntervalLimitStatus)

			var ldIntervalLimit = LogDetails{
				Name:     "间隔限制",
				NewValue: nclList[0].IntervalLimit,
				OldValue: nclList[1].IntervalLimit,
			}
			if nclList[0].IntervalLimit != nclList[1].IntervalLimit {
				ldIntervalLimit.Modify = true
			}
			ldList = append(ldList, &ldIntervalLimit)

			var ldTotalLimitStatus = LogDetails{
				Name:     "总量限制状态",
				NewValue: nclList[0].TotalLimitStatus,
				OldValue: nclList[1].TotalLimitStatus,
			}
			if nclList[0].IntervalLimitStatus != nclList[1].TotalLimitStatus {
				ldTotalLimitStatus.Modify = true
			}
			ldList = append(ldList, &ldTotalLimitStatus)

			var ldTotalLimit = LogDetails{
				Name:     "总量限制",
				NewValue: nclList[0].TotalLimit,
				OldValue: nclList[1].TotalLimit,
			}
			if nclList[0].TotalLimit != nclList[1].TotalLimit {
				ldTotalLimit.Modify = true
			}
			ldList = append(ldList, &ldTotalLimit)
		}else {
			var ldIntervalLimitStatus = LogDetails{
				Name:     "间隔限制状态",
				NewValue: nclList[0].IntervalLimitStatus,
			}
			ldList = append(ldList, &ldIntervalLimitStatus)

			var ldIntervalLimit = LogDetails{
				Name:     "间隔限制",
				NewValue: nclList[0].IntervalLimit,
			}
			ldList = append(ldList, &ldIntervalLimit)

			var ldTotalLimitStatus = LogDetails{
				Name:     "总量限制状态",
				NewValue: nclList[0].TotalLimitStatus,
			}
			ldList = append(ldList, &ldTotalLimitStatus)

			var ldTotalLimit = LogDetails{
				Name:     "总量限制",
				NewValue: nclList[0].TotalLimit,
			}
			ldList = append(ldList, &ldTotalLimit)
		}
	}

	return ldList, nil
}























